import time
from concurrent.futures import ThreadPoolExecutor, as_completed,wait,FIRST_COMPLETED


def task_cost_time(times):
    time.sleep(times)
    print(f"程序花费了{times}秒")
    return times


executor = ThreadPoolExecutor(2)

# submit的返回值就是一个Future对象
task1 = executor.submit(task_cost_time,3)
task2 = executor.submit(task_cost_time,2)

wait([task1,task2],return_when=FIRST_COMPLETED)
print('11111111')
# time_list = [3, 2, 4]
# tasks：一个Future的列表
# tasks = [executor.submit(task_cost_time,t) for t in time_list]
# as_completed() 返回的是一个生成器 里面是已经完成的future
# for f in as_completed(tasks):
#     print(f.result())

# map：map的返回值就是结果，结果的顺序和传参的顺序一样
# tasks = executor.map(task_cost_time,time_list)
# for f in tasks:
#     print(f)
"""
Future常用的方法
"""
# 判断线程是否执行完毕 （非阻塞的）
# print(task1.done())
# 获取线程的返回值 阻塞的
# print(task1.result())
# 取消线程 运行状态下或者已完成的，不允许取消
# print(task1.cancel())
